<template>
	<view class="container">
		<view class="userinfo">
			<view>
				<view class="phone">用户名：{{userData.username}}</view>
			</view>
			<image src="/static/logo.png" mode=""></image>
		</view>
		<button class="btn" @click="logout">退出登录</button>
		<view class="list">
			<view class="tab">
				<view v-for="(item,i) in navData" :class="currentIndex===i?'current':''" @click="changeIndex(i)">
					{{item}}
				</view>
			</view>
			<div class="item" v-for="item in orderData">
				<image :src="item.product[0].pimageurl" mode=""></image>
				<view>
					<view>下单时间:{{item.otime}}</view>
					<view>订单总额:￥{{item.oprice}}</view>
				</view>
			</div>
		</view>
	</view>
</template>

<script>
	import {
		queryOrder,
		queryUserInfo
	} from "@/api/index";

	export default {
		data() {
			return {
				navData: ["全部订单", "已收货", "未收货", "退订"],
				//点击切换
				currentIndex: 0,
				teaToken: "", //用户认证信息
				userData: {}, //用户信息
				orderData: [] //用户订单信息
			}
		},
		onShow() {
			this.queryUserInfo()
			this.queryOrder()

		},
		methods: {
			changeIndex(index) {
				this.currentIndex = index
				var ostate = ""
				if (index === 0) {
					ostate = ""
				} else if (index === 1) {
					ostate = "1"
				} else if (index === 3) {
					ostate = "2"
				} else if (index == 2) {
					ostate = "0"
				}

				queryOrder(ostate).then(res => {
					this.orderData = res.data
				})

			},
			queryUserInfo() {
				queryUserInfo().then(res => {
					this.userData = res.data
				})
			},
			queryOrder() {
				queryOrder("").then(res => {
					this.orderData = res.data
				})
			},
			logout() {
				uni.showModal({
					title: '提示',
					content: '你确定退出登录吗',
					success: function(res) {
						if (res.confirm) {


							uni.removeStorage({
								key: 'token',
								success: function() {
									console.log('用户 token 已删除');
									// 跳转到登录页面或其他逻辑
								},
								fail: function(err) {
									console.error('删除用户 token 失败', err);
								}
							});
							uni.showToast({
								title: '用户已退出',
								duration: 2000
							});
							setTimeout(() => {

								uni.navigateTo({
									url: "/pages/login/login"
								})
							}, 2000)

							console.log('用户点击确定');
						} else if (res.cancel) {
							uni.showToast({
								title: '用户取消退出',
								duration: 2000
							});
							console.log('用户点击取消');
						}
					}
				});
			}
		}
	}
</script>

<style scoped>
	.userinfo {
		height: 260rpx;
		background: orange;
		padding: 50rpx 30rpx;
		display: flex;
		justify-content: space-between;
		align-items: center;
	}

	.userinfo view {
		line-height: 55rpx;
		color: #999;
	}

	.userinfo image {
		width: 100rpx;
		height: 100rpx;
		border-radius: 50%;
	}

	.userinfo .phone {
		color: #fff;
		font-weight: bold;
	}

	.list {
		padding: 30rpx;
		box-sizing: border-box;
		padding-bottom: 60px;
	}

	.tab {
		display: flex;
		justify-content: space-between;
		height: 70rpx;
		align-items: center;
	}

	/* 当前被选中的样式 */
	.tab view {
		border-bottom: 1px solid #f5f5f5;
	}

	.tab .current {
		border-bottom: 1px solid #000;
	}

	.item {
		display: flex;
		padding: 15rpx;
		box-sizing: border-box;
		/* justify-content: space-between; */
		border-bottom: 1px solid #000;
		align-items: center;
	}

	.item image {
		width: 230rpx;
		height: 150rpx;
	}

	.item view {
		line-height: 50rpx;
	}

	.container {
		background: #f5f5f5;
	}
</style>